//
//  SWHDetailViewController.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/7/21.
//

import UIKit
import RxCocoa
import RxSwift
import RxGesture
import CoreLocation
import MJRefresh

class SWHDetailViewController: BaseViewController {
    fileprivate lazy var disposeBag = DisposeBag()
    var reloadPVDsListClosure: (()->Void)?//关闭detail后，要刷新installation list中对应的cell，处理cell中某些数据的改变（状态，数量，姓名等）
    let customView = SWHDetailView()
    var skipToWhere: SkipToWhere?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
        self.loadData()
        Tool().requestCurrentLocation()
    }
    //MARK: - 初始化页面
    private func initUI(){
        self.view = self.customView
        //MARK: - 设置navigation title info
        self.customView.setTitleInfo(JobDetailModelHelper.shared.jobDetailModel.owner_address.type_name, subTitle: JobDetailModelHelper.shared.jobDetailModel.gwt_id)
        self.customView.scrollView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(headerLoad(_:)))
        self.customView.loadFaildView.retryBtn.rx.tap.subscribe{ [weak self]  _ in
            guard let strongSelf = self else {return}
            strongSelf.loadData(true)
        }.disposed(by: self.disposeBag)
        //back to pre
        self.customView.backToPreBtn.rx.tap.subscribe { [weak self] _ in
            self?.backToPre()
        }.disposed(by: self.disposeBag)
        //Refresh
        self.customView.refreshBtn.rx.tap.subscribe { [weak self] _ in
            self?.loadData(true)
        }.disposed(by: self.disposeBag)
        //MARK: - edit owner info
        self.customView.topView.propertyTypeImageView.rx.tapGesture().when(.recognized).subscribe {[weak self] _ in
            guard let strongSelf = self else {return}
            if JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag {
                let editVC = EditOwnerInfomationViewController()
                editVC.hidesBottomBarWhenPushed = true
                strongSelf.navigationController?.pushViewController(editVC, animated: false)
            }
        }.disposed(by: self.disposeBag)
        //MARK: - installation type info
        self.customView.topView.installationTypeImageView.rx.tapGesture().when(.recognized).subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag {
                let editVC = EditHeaterInstallationTypeVC()
                editVC.hidesBottomBarWhenPushed = true
                strongSelf.navigationController?.pushViewController(editVC, animated: false)
            }
        }.disposed(by: self.disposeBag)
        //MARK: - topview: 编辑地址
        self.customView.topView.editAddressBtn.rx.tapGesture().when(.recognized).subscribe {[weak self] _ in
            guard let strongSelf = self else {return}
            if JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag{
                let editVC = EditInstallAddressViewController()
                editVC.refreshDetailClosure = {
                    strongSelf.loadData(false)
                }
                editVC.hidesBottomBarWhenPushed = true
                strongSelf.navigationController?.pushViewController(editVC, animated: false)
            }
        }.disposed(by: self.disposeBag)
        //MARK: - topview: call owner phone
        self.customView.topView.phoneBtn.rx.tap.subscribe { _ in
            InstallationDetailVCHelper.shared.callPhone(JobDetailModelHelper.shared.jobDetailModel.owner_address.phone)
        }.disposed(by: self.disposeBag)
        //MARK: - topview: email
        self.customView.topView.emailBtn.rx.tap.subscribe { _ in
            InstallationDetailVCHelper.shared.sendEmail(JobDetailModelHelper.shared.jobDetailModel.owner_address.email)
        }.disposed(by: self.disposeBag)
        //MARK: - topview: map navigation by  address
        self.customView.topView.mapNaviBtn.rx.tap.subscribe { [weak self]_ in
            guard let strongSelf = self else {return }
            let addressModel = DeliveryWarehouseModel()
            if JobDetailModelHelper.shared.jobDetailModel.install_address.special_address != "" {
                addressModel.special_address = JobDetailModelHelper.shared.jobDetailModel.install_address.special_address
            }else{
                let latitude = JobDetailModelHelper.shared.jobDetailModel.install_address.location.latitude
                let longitude = JobDetailModelHelper.shared.jobDetailModel.install_address.location.longitude
                addressModel.latitude = latitude
                addressModel.longitude = longitude
                addressModel.street_number = JobDetailModelHelper.shared.jobDetailModel.install_address.street_number
                addressModel.street_name = JobDetailModelHelper.shared.jobDetailModel.install_address.street_name
                addressModel.street_type = JobDetailModelHelper.shared.jobDetailModel.install_address.street_type
                addressModel.suburb = JobDetailModelHelper.shared.jobDetailModel.install_address.suburb
                addressModel.state = JobDetailModelHelper.shared.jobDetailModel.install_address.state
                addressModel.post_code = JobDetailModelHelper.shared.jobDetailModel.install_address.post_code
            }
            MapNavigationHelper.shared.showMapsOptions(addressModel,vc: strongSelf)
        }.disposed(by: self.disposeBag)
        //MARK: - pick up address: map navigation by  address
        self.customView.pickupAddressView.mapNaviBtn.rx.tap.subscribe { [weak self]_ in
            guard let strongSelf = self else {return }
            let addressModel = DeliveryWarehouseModel()
            let deliverAddressModel = JobDetailModelHelper.shared.jobDetailModel.delivery_address
            let latitude = deliverAddressModel.latitude
            let longitude = deliverAddressModel.longitude
            addressModel.latitude = latitude
            addressModel.longitude = longitude
            addressModel.street_number = deliverAddressModel.street_number
            addressModel.street_name = deliverAddressModel.street_name
            addressModel.street_type = deliverAddressModel.street_type
            addressModel.suburb = deliverAddressModel.suburb
            addressModel.state = deliverAddressModel.state
            addressModel.post_code = deliverAddressModel.post_code
            MapNavigationHelper.shared.showMapsOptions(addressModel,vc: strongSelf)
        }.disposed(by: self.disposeBag)
        //MARK: - 复制installation地址
        self.customView.topView.addressLabelLongPressGestureClosure = {[weak self] in
            guard let strongSelf = self else {return }
            InstallationDetailVCHelper.shared.copyContentToPasteBoard(content: strongSelf.customView.topView.addressLabel.text ?? "",parentView: strongSelf.customView)
        }
        //MARK: - edit system info View
        self.customView.systemInfoView.editSystemInfoBtn.rx.tapGesture().when(.recognized).subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag{
                let editVC = EditTankInfoViewController()
                editVC.refreshDetailClosure = {
                    strongSelf.loadData(false)
                }
                editVC.hidesBottomBarWhenPushed = true
                strongSelf.navigationController?.pushViewController(editVC, animated: false)
            }
        }.disposed(by: self.disposeBag)
        //MARK: - edit panel info
        self.customView.systemInfoView.tankTableView.rx.itemSelected.bind { [weak self] (_) in
            guard let strongSelf = self else {return}
            if JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag{
                let editVC = EditTankInfoViewController()
                editVC.refreshDetailClosure = {
                    strongSelf.loadData(false)
                }
                editVC.hidesBottomBarWhenPushed = true
                strongSelf.navigationController?.pushViewController(editVC, animated: false)
            }
        }.disposed(by: disposeBag)
        //MARK: - to panel list
        self.customView.checklistTableView.toTankListClickClosure = {[weak self] in
            guard let strongSelf = self else {return}
            strongSelf.toTankNumersVC()
        }
        //MARK: - to images list
        self.customView.checklistTableView.toImageListClosure = { [weak self](currentIndex) in
            guard let strongSelf = self else {return}
            strongSelf.toImageListVC(currentIndex)
        }
        //MARK: - signatures
        self.customView.checklistTableView.toSignatureClosure = { [weak self](signType) in
            guard let strongSelf = self else {return}
            strongSelf.readyToSign(signType)
        }
        //MARK: - to edit checklist text
        self.customView.checklistTableView.toEditTextClosure = { [weak self](checklistModel) in
            guard let strongSelf = self else {return}
            strongSelf.showEditCheckListText(checklistModel)
        }
        //MARK: - to edit checklist bool
        self.customView.checklistTableView.toClickChecklitBoolItemClosure = { [weak self](index) in
            guard let strongSelf = self else {return}
            if JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag{
                strongSelf.toClickBoolItemOfChecklist(index)
            }
        }
//        //MARK: - support function - Flush upload
        self.customView.supportFunctionView.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return  }
            guard  JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag else {return}
            strongSelf.flushUpload(.flushUpload)
        }).disposed(by: self.disposeBag)
        //MARK: - support
        self.customView.supportView.tableView.rx.itemSelected.bind { [weak self](indexPath) in
            guard let strongSelf = self else {return}
            let type = strongSelf.customView.supportView.dataObserver.value[indexPath.row]
            if type == SupportType.phone {
                InstallationDetailVCHelper.shared.callPhone(APPConfig.SupportPhone)
            }else if type == SupportType.email {
                InstallationDetailVCHelper.shared.sendEmail(APPConfig.SupportEmail)
            }
        }.disposed(by: disposeBag)
        //MARK: - bottom 跳转到tank 位置
        self.customView.bottomView.barcodeBtn.rx.controlEvent(.touchUpInside).subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.skipToViewPosition(R.string.localizable.preinstallationE())
        }).disposed(by: self.disposeBag)
        //MARK: - bottom 跳转到panel and inverter 位置
        self.customView.bottomView.checklistBtn.rx.controlEvent(.touchUpInside).subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.skipToViewPosition("",sectionType: .otherChecklist)//checklist用section跳转
        }).disposed(by: self.disposeBag)
        //MARK: - bottom 跳转到signature 位置
//        self.customView.bottomView.signatureBtn.rx.controlEvent(.touchUpInside).subscribe(onNext: { [weak self] _ in
//            guard let strongSelf = self else {return}
//            strongSelf.skipToViewPosition(R.string.localizable.signatureS())
//        }).disposed(by: self.disposeBag)
        //MARK: - finish job
        self.customView.finishJobBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.finishJob()
        }.disposed(by: self.disposeBag)
        //MARK: - reopen job
        self.customView.reopenJobBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.reopenJob()
        }.disposed(by: self.disposeBag)
        //MARK: - 进入Job自动上传本地数据,上传完自动刷新数据
        UploadManager.shared.uploadLocalDataByPVDJob(JobDetailModelHelper.shared.jobDetailModel.id,uploadingClosure: {[weak self]( needUploadCnt, uploadSuccessCnt, uploadFaildCnt) in
            guard let strongSelf = self else {return}
            if  needUploadCnt == (uploadSuccessCnt + uploadFaildCnt){
                strongSelf.loadData(false)
            }
        })
    }
    override func backToPre() {
        self.reloadPVDsListClosure?()//刷新list中status，数量，地址等的变化，detail中可能
        self.navigationController?.popViewController(animated: true)
    }
    //MARK: - 加载数据
    private func loadData(_ loadingFlag: Bool = true, header: MJRefreshNormalHeader? = nil){
        //set block
        let viewModel = JobDetailService()
        viewModel.setBlockAfterNetwork({ [weak self](result , otherInfo) -> Void in
            guard let strongSelf = self else {return}
            if loadingFlag {
                ModelViewUtil.shared.hideLoadingView()
            }
            if let _ = header{
                header?.endRefreshing()
            }
            if strongSelf.customView.scrollView.isHidden {//防止缓存数据加载一次，网络数据又加载一次，导致skipToWhere后跳转不准确
                strongSelf.customView.showDetailView()
            }
            //是否跳转到的panel tab 页面，跳转完成后置空
            if let toWhere = strongSelf.skipToWhere {
                switch toWhere {
                case .Barcode:
                    strongSelf.skipToViewPosition(R.string.localizable.preinstallationE())
                case .Checklist:
                    strongSelf.skipToViewPosition("",sectionType: .otherChecklist)
                case .Signature:
                    strongSelf.skipToViewPosition(R.string.localizable.signatureS())
                default:
                    break
                }
                strongSelf.skipToWhere = nil
            }
            
        }) { (code, error) -> Void in
            if loadingFlag {
                ModelViewUtil.shared.hideLoadingView()
            }
            if code == APICustomStatusCode.RequestFaild.rawValue{//网络请求错误才显示Retry
                self.customView.showFaildView()
            }
            Tool().showErrorInfo(code, error)
        }
        if loadingFlag {
            //add Loading view
            ModelViewUtil.shared.showLoadingView()
        }
        viewModel.getInstallationsDetail(cacheFlag: true)
    }
    @objc func headerLoad(_ hearder: MJRefreshNormalHeader){
        self.loadData(false,header: hearder)
    }
    //MARK: - to tank list(和inverter共用一个)
    private func toTankNumersVC(_ animate: Bool = true) {
        let vc = JobDetailInvertersListViewController()
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: animate)
    }
    //MARK: - 跳转到Image list
    private func toImageListVC(_ index: Int){
        guard index < JobDetailModelHelper.shared.jobDetailModel.pvd_checklists.count else {
            return
        }
        //如果当前checklist是mid-installation check-up或者testing and commissioning checklist而且上一步没有完成
        let serverModel = JobDetailModelHelper.shared.jobDetailModel.pvd_checklists[index]
        let checklistID = serverModel.id
        if checklistID == ChecklistFixedId.midInstall.rawValue && !JobDetailModelHelper.shared.jobDetailModel.checkInSelfieDoneFlag {
            let alertMsg = "Please take '\(JobDetailModelHelper.shared.jobDetailModel.checkInSelfieTitle)'"
            Tool().showAlertViewWithOneBtn(title: "Temporary Locked",message: alertMsg, confirmStr: "Dismiss")
        }else if checklistID == ChecklistFixedId.testAndCommission.rawValue && !JobDetailModelHelper.shared.jobDetailModel.midInstallationSelfieDoneFlag{
            let alertMsg = "Please take '\(JobDetailModelHelper.shared.jobDetailModel.midInstallationSelfieTitle)'"
            Tool().showAlertViewWithOneBtn(title: "Temporary Locked",message: alertMsg, confirmStr: "Dismiss")
        }else{
            self.forwardToImageListVC(index)
        }
    }
    private func forwardToImageListVC(_ index: Int){
        let vc =  JobImageListViewController()
        JobDetailModelHelper.shared.checklistModel = JobDetailModelHelper.shared.jobDetailModel.pvd_checklists[index]
        JobDetailModelHelper.shared.checklistModel.pvdId = JobDetailModelHelper.shared.jobDetailModel.id
        self.navigationController?.pushViewController(vc,animated: false)
    }
    fileprivate func showEditCheckListText(_ serverModel: PVDChecklistModel){
        let vc = EditChecklistTextViewController()
        serverModel.pvdId = JobDetailModelHelper.shared.jobDetailModel.id
        vc.serverModel = serverModel
        vc.saveClosure = { (checkModel) in
            //0.必须先插入数据库，因为broadCast时，有地方（flush upload）会用到本地数据的统计
            DBManager.shared.insertChecklistData(model: checkModel)
            checkModel.oper_type = DataOperType.Add.rawValue
            //1.增加到当前list中，刷新数据
            if let index = JobDetailModelHelper.shared.jobDetailModel.pvd_checklists.firstIndex(where: {$0.id == checkModel.id}){
                JobDetailModelHelper.shared.jobDetailModel.pvd_checklists[index] = checkModel
                JobDetailModelHelper.shared.jobDetailModel.broadCastChecklistData()
            }
            //2.上传
            UploadManager.shared.uploadChecklistBoolOrText(checklistModel: checkModel)
        }
        self.navigationController?.pushViewController(vc, animated: true)
    }
    private func toClickBoolItemOfChecklist(_ index: Int){
        let currentCheckListModel = JobDetailModelHelper.shared.jobDetailModel.pvd_checklists[index]
        currentCheckListModel.pvdId  = JobDetailModelHelper.shared.jobDetailModel.id
        //1.local data exist
        if currentCheckListModel.answer == "true" {
            currentCheckListModel.answered_at = ""
            currentCheckListModel.answer = ""
            currentCheckListModel.full_name = ""
        }else{
            currentCheckListModel.answered_at = DateUtil().getCurrentDatetime()
            currentCheckListModel.answer = "true"
            currentCheckListModel.full_name = APPConfig.loginInfo.fullName
        }
        //0.必须先插入数据库，因为broadCastChecklistData时，有地方会用到本地数据
        DBManager.shared.insertChecklistData(model: currentCheckListModel)
        currentCheckListModel.oper_type = DataOperType.Add.rawValue
        //1.增加到当前list中，刷新数据
        JobDetailModelHelper.shared.jobDetailModel.pvd_checklists[index] = currentCheckListModel
        JobDetailModelHelper.shared.jobDetailModel.broadCastChecklistData()
        //3.上传
        UploadManager.shared.uploadChecklistBoolOrText(checklistModel: currentCheckListModel)
    }
    //MARK: - 跳转到某个位置部分， scan，signature，checklist
    fileprivate func skipToViewPosition(_ title: String, sectionType: CheckListSectionType? = nil){
        if let currentSection = sectionType, let row = JobDetailModelHelper.shared.jobDetailModel.pvd_checklists.firstIndex(where: {$0.section == currentSection && ($0.type == ChecklistType.headerWithImage.rawValue || $0.type == ChecklistType.header.rawValue)}) {//other checklist
            //跳转到scan部分
            let recOnTableView = self.customView.checklistTableView.rectForRow(at: IndexPath(row: row, section: 0))
            let cellPointOnScrollView = self.customView.checklistTableView.convert(recOnTableView.origin, to: self.customView.scrollView)
            self.customView.scrollView.scrollToView(point: cellPointOnScrollView, animated: true)
        }else if let row = JobDetailModelHelper.shared.jobDetailModel.pvd_checklists.firstIndex(where: {$0.title == title && ($0.type == ChecklistType.headerWithImage.rawValue || $0.type == ChecklistType.header.rawValue)}){
            //跳转到scan部分
            let recOnTableView = self.customView.checklistTableView.rectForRow(at: IndexPath(row: row, section: 0))
            let cellPointOnScrollView = self.customView.checklistTableView.convert(recOnTableView.origin, to: self.customView.scrollView)
            self.customView.scrollView.scrollToView(point: cellPointOnScrollView, animated: true)
        }
    }
    //MARK: - reopen job
    private func reopenJob(){
        if JobDetailModelHelper.shared.jobDetailModel.isInstallerFlag {//installer才能reopen job
            let vm = JobDetailService()
            vm.setBlockAfterNetwork({ (result , _) -> Void in
                ModelViewUtil.shared.hideLoadingView()
                JobDetailModelHelper.shared.jobDetailModel.job_status = PVDJobStatusValue.installing.rawValue
                Tool().showAlertViewWithOneBtn(message: NSLocalizedString("pvd.detail.reopenedJob", comment: "")) {
                    InstallationDetailVCHelper.shared.cacheData()
                }
            }) {(code, error) -> Void in
                ModelViewUtil.shared.hideLoadingView()
                Tool().showErrorInfo(code, error)
                
            }
            //add Loading view
            ModelViewUtil.shared.showLoadingView(NSLocalizedString("pvd.detail.reopenJob", comment: ""))
            vm.reopenJob()
        }
    }
    //MARK: - 上传job的 本地数据，且显示uploading 信息
    private func flushUpload(_ toContinueType: FlushUploadType){
        if Connectivity.isConnectedToInternet  {
            if JobDetailModelHelper.shared.jobDetailModel.detailModifyFlag{
                let view = FlushUploadView()
                self.navigationController?.view.addSubview(view)
                view.snp.makeConstraints { make in
                    make.edges.equalToSuperview()
                }
                //停止Flush reupload
                view.stopFlushUploadClosure = {[weak self] in
                    guard let strongSelf = self else {return }
                    UploadManager.shared.cancelFlushUpload()
                    strongSelf.loadData(false)
                    view.removeFromSuperview()
                }
                
                UploadManager.shared.uploadLocalDataByPVDJob(JobDetailModelHelper.shared.jobDetailModel.id, uploadingClosure: { [weak self] (needUploadCnt,uploadSuccessCnt, uploadFaildCnt) in
                    guard let strongSelf = self else {return}
                    view.uploadingLabel.text = "Uploading \(uploadSuccessCnt)/\(needUploadCnt)"
                    if needUploadCnt == uploadSuccessCnt + uploadFaildCnt {//上传完毕
                        strongSelf.loadData(false)
                        if let _ = view.superview {//可能stopFlushUpload时候被remove掉了，所以此处要判断下，保持数据统一
                            view.removeFromSuperview()
                        }
                        if uploadSuccessCnt == needUploadCnt  {//全部上传成功
                            if toContinueType == .flushUpload {//单纯的上传
                                //无代码处理
                            }else{//finish job , panel installed
                                strongSelf.loadDataAndToContinue(toContinueType)
                            }
                        }else{//部分数据，上传失败
                            Tool().showAlertViewWithTwoBtns(title: "Partial upload failed", message: "Some data failed to upload, please upload again.",cancelStr: "Cancel", confirmStr: "Re-upload",noClosure: nil, yesClosure: {
                                strongSelf.flushUpload(.flushUpload)
                            })
                        }
                    }
                })
            }
        }else{
            Tool().showFailureInfo()
        }
    }
    //MARK: 用户finish job，panel installed上传完本地数据后，拉取最新数据，继续走finish job或panel installed的流程
    private func loadDataAndToContinue(_ toContinueType: FlushUploadType) {
        let vm = JobDetailService()
        //set block
        vm.setBlockAfterNetwork({ (result , _) -> Void in
            ModelViewUtil.shared.hideLoadingView()
            //本地数据上传完毕后
            if  toContinueType == .finishJob {//continue finish job
                self.finishJob()
            }
        }) { (code, error) -> Void in
            ModelViewUtil.shared.hideLoadingView()
            self.customView.showFaildView()
            Tool().showErrorInfo(code, error)
        }
        //add Loading view
        if toContinueType == .finishJob {//finish job
            ModelViewUtil.shared.showLoadingView(NSLocalizedString("pvd.detail.finishJob", comment: ""))
        }
        vm.getInstallationsDetail()
    }
    //MARK: - finish job
    private func finishJob(){
        if !Connectivity.isConnectedToInternet {
            Tool().showFailureInfo()
            return
        }
        if JobDetailModelHelper.shared.jobDetailModel.isInstallerFlag {//installer才能finish job
            //local data need to upload before finish job
            if JobDetailModelHelper.shared.jobDetailModel.detailModifyFlag {
                Tool().showAlertViewWithTwoBtns(message: NSLocalizedString("pvd.detail.localdata.check.uploaded", comment: ""), cancelStr: "Cancel", confirmStr: "Upload&Continue", yesClosure:  {
                    self.flushUpload(.finishJob)
                })
            }else{
                InstallationDetailVCHelper.shared.cacheJobDetail(NSLocalizedString("pvd.detail.finishJob", comment: "")) {
                    self.requestFinishJobAPI(APPConfig.currentLocation.combineStr)
                } faildClosure: { code, error in
                    Tool().showErrorInfo(code,error)
                }
            }
        }
    }
    
    //MARK: 请求finish job api
    private func requestFinishJobAPI(_ loactionStr: String = ""){
        if Connectivity.isConnectedToInternet {
            //校验checklit，signatures，所有的必填项，（tank numbers不校验必填项）
            if InstallationDetailVCHelper.shared.checkRequiredWhenFinishJobForSWH() {
                let vm = JobDetailService()
                vm.setBlockAfterNetwork({ (result , _) -> Void in
                    ModelViewUtil.shared.hideLoadingView()
                    //必须缓存Job status的数据，防止无网络刷新数据又显示finish job的问题
                    JobDetailModelHelper.shared.jobDetailModel.job_status = PVDJobStatusValue.installed.rawValue
                    InstallationDetailVCHelper.shared.cacheData()

                    Tool().showAlertViewWithOneBtn(message: NSLocalizedString("pvd.detail.finishedJob", comment: ""))
                }) { (code, error) -> Void in
                    ModelViewUtil.shared.hideLoadingView()
                    Tool().showErrorInfo(code,error)
                }
                //add Loading view
                ModelViewUtil.shared.showLoadingView(NSLocalizedString("pvd.detail.finishJob", comment: ""))
                vm.finishJob(loactionStr)
            }
        }else{
            Tool().showFailureInfo()
        }
        
    }
    //MARK: - 准备去签名
    private func readyToSign(_ signType: SignatureType){
        if JobDetailModelHelper.shared.jobDetailModel.isInstallerFlag{//installer可以重签所有的签名
            if InstallationSignatureService().setDataInfo(self).checkIfNeedResign(signType) {
                let vc = ResignViewController()
                vc.signatureType = signType
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                InstallationSignatureService().setDataInfo(self).toSignature(signType)
            }
        }
    }
    override var preferredStatusBarStyle: UIStatusBarStyle {
        if #available(iOS 13.0, *) {
            return .darkContent
        } else {
            return .default
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
}
